<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2020/06/04 9:44
 * Desc: 微信小程序优惠券处理
 */
class Model_Api_Standard_Coupon
{

    /**
     * @function 判断会员优惠券的领取情况
     * @param $cid
     * @param $mid
     * @param null $totalnum
     * @param null $maxnum
     * @return array int 1,可以领取 2:已经领完 3：已经领取，待使用 4:会员等级不满足
     */
    public static function check_receive_status($cid,$mid = null,$totalnum = null, $maxnum = null)
    {
        $memeber_grades = DB::select('memeber_grades')->from('coupon')->where("id=$cid and isopen=1 and isdel=0")->execute()->get('memeber_grades');
        if(!$memeber_grades)
        {
            return array('status' => 2, 'surplus' => 0);
        }

        if (!$totalnum || !$maxnum)
        {
            $data = DB::select('totalnumber','maxnumber')->from('coupon')->where('id', '=', $cid)->execute()->current();
            $totalnum = $data['totalnumber'];
            $maxnum = $data['maxnumber'];
        }
        $sql = " select count(*) as usernum  from sline_member_coupon where cid = $cid";
        $userdata = DB::query(1, $sql)->execute()->current();
        if (empty($userdata) || $userdata['usernum'] < $totalnum)
        {
            $status = '1';
            $surplus = $totalnum - $userdata['usernum'];//剩余数量
        }
        else
        {
            $status = '2';
            $surplus = 0;
        }

        //会员信息
        if (!empty($mid))
        {
            $sql = "select count(*) as totalnum from sline_member_coupon WHERE mid={$mid} and cid=$cid";
            $infodata = DB::query(1,$sql)->execute()->current();
            if ($infodata['totalnum'] >= $maxnum)
            {
                $status = 3;
                $surplus = $maxnum;
            }
            else
            {
                $member_rank = self::get_member_rank($mid);
                $memeber_grades = explode(',',$memeber_grades);
                if(!in_array($member_rank['id'],$memeber_grades))
                {
                    $status = 4;
                }
            }
        }
        return array('status' => $status, 'surplus' => $surplus);
    }

    /**
     * @function 会员等级
     * @param $memberId
     * @return mixed
     */
    public static function get_member_rank($memberId)
    {
        $rank = DB::select()->from('member_grade')->order_by('begin', 'asc')->execute()->as_array();
        if(!empty($memberId))
        {
            $k = 0;
            $member=DB::query(1,'select sum(jifen) as jifen from sline_member_jifen_log where  type=2 and memberid='.$memberId)->execute()->current();
            $range = array();
            foreach ($rank as $k => $v) {
                $range[] = $v['begin'];
            }
            $rangeLevel = count($range);
            if ($member['jifen'] < $range[0]) {
                $k = 0;
            } else if ($member['jifen'] > $range[$rangeLevel - 1]) {
                $k = $rangeLevel - 1;
            } else {
                foreach ($range as $k => $v) {
                    if ($member['jifen'] < $v) {
                        --$k;
                        break;
                    }
                }
            }
            $grade = $rank[$k];
            $grade['current'] = ++$k;
        }
        return $grade;
    }

}